library _;

import 'package:flutter/material.dart';
import 'package:flutter_app/Page/Main/Other/ShowWeb/jsCallBack.dart';
import 'package:flutter_app/View/MyLoader.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:webviewx_plus/webviewx_plus.dart';

class ShowWebPage extends YPage {
  ShowWebPage(this.title, this.url);

  String? title;
  String url;

  WebViewXController? webViewController;
  double progress = 0.1;

  @override
  void yOnCreate() {
    yRun(() {
      if (progress < 1) {
        setState(() => progress = 0.5);
        yRun(() {
          if (progress < 1) setState(() => progress = 0.8);
        }, delay: 500);
      }
    }, delay: 200);
  }

  @override
  void yOnDestory() {
    progress = 0;
    webViewController?.dispose();
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: [
          title != null ? YAppBar(title: title) : YEmpty(),
          Stack(
            alignment: Alignment.center,
            children: [
              WebViewX(
                width: yScreenSize().width,
                height: yScreenSize().height - yStatubarHeight() - yNavBarHeight() - ySafeAreaBottomHeight(),
                dartCallBacks: onDartCallBacks(),
                initialContent: url,
                initialSourceType: SourceType.url,
                onPageStarted: (src) => setState(() => progress = 1),
                onWebViewCreated: (controller) {
                  webViewController = controller;
                },
              ),
              Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: progress >= 1
                    ? YEmpty()
                    : LinearProgressIndicator(
                        color: YConfig.themeColor,
                        backgroundColor: Colors.white,
                        minHeight: 3,
                        value: progress,
                      ),
              ),
              progress >= 1 ? YEmpty() : MyLoader(),
            ],
          ),
        ],
      ),
    );
  }
}
